<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.code" placeholder="股票代码" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.name" placeholder="股票名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.pinyin" placeholder="股票拼音" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.id" placeholder="ID" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.market" style="width: 140px" class="filter-item" @change="handleFilter">
        <el-option v-for="item in marketOptions" :key="item.key" :label="item.label" :value="item.key" />
      </el-select>

      <el-select v-model="listQuery.state" style="width: 140px" class="filter-item" @change="handleFilter">
        <el-option v-for="item in stateOptions" :key="item.key" :label="item.label" :value="item.key" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button type="primary" size="mini" @click="syncAllStock">
        全部同步<i class="el-icon-refresh" />

      </el-button>
      <el-button type="primary" size="mini" @click="updateAllStockInfo">
        全部更新<i class="el-icon-refresh" />

      </el-button>

    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" prop="id" sortable="custom" align="center" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="股票名称" prop="name" sortable="custom" align="center" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="股票代码" prop="code" sortable="custom" align="center" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.code }}</span>
        </template>
      </el-table-column>
      <el-table-column label="股票拼音" prop="spell" sortable="custom" align="center" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.spell }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" sortable="custom" align="center" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.status==1 ">启用</span>
          <span v-if="scope.row.status==0 ">禁用</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button v-if="row.status==0" type="primary" size="mini" @click="banStock(row.id)">
            启用
          </el-button>
          <el-button v-if="row.status==1" size="mini" type="danger" @click="banStock(row.id)">
            禁用
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.size" @pagination="getList" />

    <el-dialog :visible.sync="dialogPvVisible" title="Reading statistics">
      <el-table :data="pvData" border fit highlight-current-row style="width: 100%">
        <el-table-column prop="key" label="Channel" />
        <el-table-column prop="pv" label="Pv" />
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogPvVisible = false">Confirm</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// import { fetchList, fetchPv, createArticle, updateArticle } from '@/api/article'
import { pageList, banStock, syncAllStock, updateAllStockInfo } from '@/api/stock/stockInfo'
import waves from '@/directive/waves' // waves directive
// import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
    // statusFilter(status) {
    //   const statusMap = {
    //     published: 'success',
    //     draft: 'info',
    //     deleted: 'danger'
    //   }
    //   return statusMap[status]
    // },
    // typeFilter(type) {
    //   return calendarTypeKeyValue[type]
    // }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        currentPage: 1,
        size: 20,
        code: null,
        name: null,
        pinyin: null,
        id: null,
        market: null,
        state: null
      },
      marketOptions: [{ label: '深圳', key: 'sz' }, { label: '上海', key: 'sh' }],
      stateOptions: [{ label: '可用', key: '1' }, { label: '禁用', key: '0' }],
      showReviewer: false,
      temp: {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        type: '',
        status: 'published'
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      console.log(this.listQuery)
      pageList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.currentPage = 1
      this.getList()
    },
    // 禁用个股
    banStock(id) {
      banStock(id).then(response => {
        this.getList()
        this.$message({
          message: response.msg,
          type: 'success'
        })
      })
    },
    // 同步所有列表
    syncAllStock() {
      syncAllStock().then(response => {
        this.$message({
          message: response.msg,
          type: 'success'
        })
      })
    },
    // 更新所有数据
    updateAllStockInfo() {
      updateAllStockInfo().then(response => {
        this.$message({
          message: response.msg,
          type: 'success'
        })
      })
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        status: 'published',
        type: ''
      }
    }
  }
}
</script>
